import Vue from 'vue'
import Vuex from 'vuex'

import {
  spiderTaskQuery,
  spiderTaskDelete,
  spiderTaskAdd,
  spiderTaskUpdate,
  spiderTaskAddPlan,
  spiderTaskRun,
  spiderTaskStart,
  spiderTaskStop,
} from '@/api/admin/collect/spiderTask'

Vue.use(Vuex)

const state = {
  taskRow: {},
}

const getters = {}

const actions = {
  //试题增删改查
  async queryTestPaper({ commit }, listQuery) {
    let res = await spiderTaskQuery({ ...listQuery })
    return res.data.code == 0 ? res.data : Promise.reject(new Error('faile'))
  },
  async deleteTestPaper({ commit }, row) {
    let res = await spiderTaskDelete(row.id)
    return res.data.code == 0 ? res.data : Promise.reject(new Error('faile'))
  },
  async addTestPaper({ commit }, data) {
    let res = await spiderTaskAdd(data)
    return res.data.code == 0 ? res.data : Promise.reject(new Error('faile'))
  },
  async updateTestPaper({ commit }, data) {
    let res = await spiderTaskUpdate(data)
    return res.data.code == 0 ? res.data : Promise.reject(new Error('faile'))
  },
  //执行采集
  async runTestPaper({ commit }, row) {
    let res = await spiderTaskRun(row.id)
    return res.data.code == 0 ? res.data : Promise.reject(new Error('faile'))
  },
  //启动定时任务
  async startTestPaper({ commit }, row) {
    let res = await spiderTaskStart(row.id)
    return res.data.code == 0 ? res.data : Promise.reject(new Error('faile'))
  },
  //停止定时任务
  async stopTestPaper({ commit }, row) {
    let res = await spiderTaskStop(row.id)
    return res.data.code == 0 ? res.data : Promise.reject(new Error('faile'))
  },
  //添加采集计划
  async addPlanTestPaper({ commit }, data) {
    let res = await spiderTaskAddPlan(data)
    return res.data.code == 0 ? res.data : Promise.reject(new Error('faile'))
  },
}

const mutations = {
  SET_TASKROW: (state, d) => {
    state.taskRow = Object.assign(state.taskRow, d)
  },
  CLEAR_TASKROW: (state) => {
    state.taskRow = {}
  },
}

export default {
  state,
  getters,
  actions,
  mutations,
}
